package com.ruoyi.factorydata.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.factorydata.domain.dto.SupplierDataDTO;
import com.ruoyi.factorydata.domain.vo.SupplierDataVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.SupplierData;
import com.ruoyi.factorydata.service.ISupplierDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商数据Controller
 * 
 * @author ruoyi
 * @date 2025-06-30
 */
@RestController
@RequestMapping("/supplierdata/supplier")
public class SupplierDataController extends BaseController
{
    @Autowired
    private ISupplierDataService supplierDataService;

    /**
     * 查询供应商数据列表
     */
    @PreAuthorize("@ss.hasPermi('supplierdata:supplier:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SupplierDataDTO supplierDataDTO)
    {
        startPage();
        List<SupplierDataVo> list = supplierDataService.selectSupplierDataList(supplierDataDTO);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 导出供应商数据列表
     */
    @PreAuthorize("@ss.hasPermi('supplierdata:supplier:export')")
    @Log(title = "供应商数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupplierDataDTO supplierDataDTO)
    {
        List<SupplierDataVo> list = supplierDataService.selectSupplierDataList(supplierDataDTO);
        ExcelUtil<SupplierDataVo> util = new ExcelUtil<SupplierDataVo>(SupplierDataVo.class);
        util.exportExcel(response, list, "供应商数据数据");
    }

    /**
     * 获取供应商数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplierdata:supplier:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(supplierDataService.selectSupplierDataById(id));
    }

    /**
     * 新增供应商数据
     */
    @PreAuthorize("@ss.hasPermi('supplierdata:supplier:add')")
    @Log(title = "供应商数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupplierDataDTO supplierDataDTO)
    {
        boolean exists = supplierDataService.lambdaQuery()
            .eq(SupplierData::getSupplierCode, supplierDataDTO.getSupplierCode()).count() > 0;
        if (exists){
                return error("供应商编码已存在");
        }
        boolean exists1 = supplierDataService.lambdaQuery()
                        .eq(SupplierData::getTaxId, supplierDataDTO.getTaxId()).count() > 0;
        if (exists1){
            return error("供应商税号已存在");
        }
        supplierDataDTO.setCreateBy(getUserId().toString());
        return toAjax(supplierDataService.insertSupplierData(supplierDataDTO));
    }

    /**
     * 修改供应商数据
     */
    @PreAuthorize("@ss.hasPermi('supplierdata:supplier:edit')")
    @Log(title = "供应商数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupplierDataDTO supplierDataDTO)
    {
        if (supplierDataDTO.getId() == null){
            return error("供应商数据不存在");
        }
        boolean exists = supplierDataService.lambdaQuery()
                .ne(SupplierData::getId, supplierDataDTO.getId())
                .eq(SupplierData::getSupplierCode, supplierDataDTO.getSupplierCode())
                .count() > 0;
        if (exists){
            return error("供应商编码已存在");
        }
        boolean exists1 = supplierDataService.lambdaQuery()
                .ne(SupplierData::getId, supplierDataDTO.getId())
                .eq(SupplierData::getTaxId, supplierDataDTO.getTaxId()).count() > 0;
        if (exists1){
            return error("供应商税号已存在");
        }
        return toAjax(supplierDataService.updateSupplierData(supplierDataDTO));
    }

    /**
     * 删除供应商数据
     */
    @PreAuthorize("@ss.hasPermi('supplierdata:supplier:remove')")
    @Log(title = "供应商数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(supplierDataService.deleteSupplierDataByIds(ids));
    }
}
